package com.leo.lime.music.content.me.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.leo.lime.music.R;
import com.leo.lime.music.content.me.adapter.PromotionAdapter;
import com.leo.lime.music.content.me.domain.Promotion;
import com.leo.lime.music.controller.MusicController;

import java.util.ArrayList;

public class MeFragment extends Fragment {
    private static final String TAG = "MeFragment";

    private MusicController mMusicController;

    private View mLocalView;
    private View mLocalConnectionView;
    private View mLocalDownloadView;
    private View mLocalRecentsView;

    private ExpandableListView mPromotionELV;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mMusicController = MusicController.getInstance(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.content_me_view, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        FragmentTransaction fm = getActivity().getSupportFragmentManager().beginTransaction();
        fm.add(R.id.personal_frags, new UserInfoFragment());
        fm.commit();

        mLocalView = view.findViewById(R.id.local_view);
        setupLocalView(mLocalView, R.drawable.ic_phone_local, R.string.content_me_local_music, getLocalMusicNum());
        mLocalConnectionView = view.findViewById(R.id.local_connection);
        setupLocalView(mLocalConnectionView, R.drawable.ic_keep, R.string.content_me_local_connection, getLocalConnectionNum());
        mLocalDownloadView = view.findViewById(R.id.local_download);
        setupLocalView(mLocalDownloadView, R.drawable.ic_download, R.string.content_me_local_download, getLocalDownloadNum());
        mLocalRecentsView = view.findViewById(R.id.local_recents);
        setupLocalView(mLocalRecentsView, R.drawable.ic_recent, R.string.content_me_local_recents, getLocalRecentsNum());

        mPromotionELV = view.findViewById(R.id.content_me_promotion);
        PromotionAdapter cmpa = new PromotionAdapter(this.getActivity(), getPromotionList());
        mPromotionELV.setAdapter(cmpa);
        mPromotionELV.setGroupIndicator(null);
    }

    private void setupLocalView(View view, int drawableId, int localTextId, String localNum) {
        ViewHolder holder;
        if(view.getTag() == null) {
            holder = new ViewHolder();
            holder.icon = view.findViewById(R.id.content_me_local_iv);
            holder.localTv = view.findViewById(R.id.content_me_local_tv);
            holder.numTv = view.findViewById(R.id.content_me_local_num_tv);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.icon.setImageDrawable(getResources().getDrawable(drawableId));
        holder.localTv.setText(getResources().getText(localTextId));
        holder.numTv.setText(localNum);
    }

    private String getLocalMusicNum() {
        return "0";
    }

    private String getLocalConnectionNum() {
        return "0";
    }

    private String getLocalDownloadNum() {
        return "0";
    }

    private String getLocalRecentsNum() {
        return "0";
    }

    private ArrayList<Promotion> getPromotionList() {
        ArrayList<Promotion> promotions = new ArrayList<Promotion>();
        Promotion promotion1 = new Promotion();
        promotion1.setTitle("aaa");
        Promotion promotion2 = new Promotion();
        promotion2.setTitle("bbb");
        Promotion promotion3 = new Promotion();
        promotion3.setTitle("ccc");
        Promotion promotion4 = new Promotion();
        promotion4.setTitle("ddd");
        promotions.add(promotion1);
        promotions.add(promotion2);
        promotions.add(promotion3);
        promotions.add(promotion4);
        return promotions;
    }

    private class ViewHolder {
        ImageView icon;
        TextView localTv;
        TextView numTv;
    }
}
